package com.inossem.warehousestorage_inossem.adapter.receive;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.receive.ReceiveMaterielDetailActivity;
import com.inossem.warehousestorage_inossem.activity.receive.ReceiveReceiveItemActivity;
import com.inossem.warehousestorage_inossem.activity.receive.extra.ReceiveExtra;
import com.inossem.warehousestorage_inossem.bean.receive.ReceiveReceiveItemBean;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;
import com.inossem.warehousestorage_inossem.utils.ReceiveUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class ReceiveItemListAdapter extends BaseAdapter {

    private Context mContext;

    private List<ReceiveReceiveItemBean.DataBean.GoodsListBean> mGoodsList;

    private LayoutInflater inflater;

    private String mGoodsNumber;

    private boolean mReadOnly;

    private ReceiveReceiveItemActivity.ReceiveReceiveItemListener mReceiveReceiveDetailListener;

    public ReceiveItemListAdapter(Context context, List<ReceiveReceiveItemBean.DataBean.GoodsListBean> goodsList, String goodsNumber, boolean readOnly, ReceiveReceiveItemActivity.ReceiveReceiveItemListener receiveReceiveDetailListener) {
        mContext = context;
        mGoodsList = goodsList;
        mGoodsNumber = goodsNumber;
        mReadOnly = readOnly;
        mReceiveReceiveDetailListener = receiveReceiveDetailListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mGoodsList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mGoodsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_receive_item_list_first_item, null);
            EditText goodsNumber = (EditText) convertView.findViewById(R.id.goodsNumber);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            goodsNumber.setText(mGoodsNumber);
            goodsNumber.addTextChangedListener(new QRCodeEditTextWatcher(goodsNumber) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, goodsNumber);
                    mReceiveReceiveDetailListener.onNoDoubleClickListener(text);
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    mReceiveReceiveDetailListener.onNoDoubleClickListener(goodsNumber.getText().toString());
                }
            });
        } else {
            ReceiveReceiveItemBean.DataBean.GoodsListBean goodsItem = mGoodsList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_receive_item_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, goodsItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.serialNumber = (TextView) view.findViewById(R.id.serialNumber);
        viewHolder.materiel = (TextView) view.findViewById(R.id.materiel);
        viewHolder.materielDescribe = (TextView) view.findViewById(R.id.materielDescribe);
        viewHolder.receiveDate = (TextView) view.findViewById(R.id.receiveDate);
        viewHolder.deliveryNumber = (TextView) view.findViewById(R.id.deliveryNumber);
        viewHolder.receiveNumber = (TextView) view.findViewById(R.id.receiveNumber);
        viewHolder.headLayout = (PercentRelativeLayout) view.findViewById(R.id.headLayout);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, ReceiveReceiveItemBean.DataBean.GoodsListBean goodsItem) {
        viewHolder.serialNumber.setText(goodsItem.getSerialNumber());
        viewHolder.materiel.setText(goodsItem.getMateriel());
        viewHolder.materielDescribe.setText(goodsItem.getMaterielDescribe());
        if (TextUtils.isEmpty(goodsItem.getReceiveDate())) {
            viewHolder.receiveDate.setText(mReadOnly ? "" : context.getResources().getString(R.string.activity_receive_receive_item_receive_date));
            viewHolder.receiveDate.setTextColor(ContextCompat.getColor(context, R.color.activity_receive_delivery_overview_font_color));

        } else {
            viewHolder.receiveDate.setText(goodsItem.getReceiveDate());
            viewHolder.receiveDate.setTextColor(ContextCompat.getColor(context, R.color.common_font_content));
        }

        viewHolder.deliveryNumber.setText(context.getResources().getString(R.string.activity_receive_receive_item_delivery) + goodsItem.getDeliveryNum() + context.getResources().getString(R.string.common_space) + goodsItem.getUnit());
        if (TextUtils.isEmpty(goodsItem.getReceiveNum())) {
            viewHolder.receiveNumber.setText(mReadOnly ? "" : context.getResources().getString(R.string.activity_receive_receive_item_receive_number));
            viewHolder.receiveNumber.setTextColor(ContextCompat.getColor(context, R.color.activity_receive_delivery_overview_font_color));
        } else {
            viewHolder.receiveNumber.setText(context.getResources().getString(R.string.activity_receive_receive_item_receive) + goodsItem.getReceiveNum() + context.getResources().getString(R.string.common_space) + goodsItem.getUnit());
            viewHolder.receiveNumber.setTextColor(ContextCompat.getColor(context, R.color.common_font_content));
        }

        if (ReceiveUtils.checkGoodsItem(goodsItem)) {
            viewHolder.headLayout.setBackgroundResource(R.color.test_green);
        } else {
            viewHolder.headLayout.setBackgroundResource(R.color.view_receive_item_list_dark_row);
        }

        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, ReceiveMaterielDetailActivity.class);
                intent.putExtra(ReceiveExtra.SERIAL_NUMBER, goodsItem.getSerialNumber());
                if (mReadOnly) {
                    intent.putExtra(ReceiveExtra.READ_ONLY, true);
                }
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView serialNumber, materiel, materielDescribe, receiveDate, deliveryNumber, receiveNumber;

        private PercentRelativeLayout headLayout;
    }

}
